using System;
using Facebook;

public class MyFB
{
    private FacebookAPI myFBAPI;
    private string appId = "106732246076055";
    private string apiKey = "93480d7507dc3e43e1cdf46383635197";
    private string appSecret = "cad2e542580cb029262076b019003bff";

    #region Application Settings Properties
    /// <summary>
    /// Get And Set Application ID
    /// </summary>
    public String ApplicationID
    {
        get
        {
            return appId;
        }
        set
        {
            appId = value;
        }
    }

    /// <summary>
    /// Get And Set API Key
    /// </summary>
    public String APIKey
    {
        get
        {
            return apiKey;
        }
        set
        {
            apiKey = value;
        }
    }

    /// <summary>
    /// Get And Set Application Secret
    /// </summary>
    public String ApplicationSecret
    {
        get
        {
            return appSecret;
        }
        set
        {
            appSecret = value;
        }
    }

    /// <summary>
    /// Get And Set Access Token
    /// </summary>
    public String AccessToken
    {
        get
        {
            return myFBAPI.AccessToken;
        }
        set
        {
            myFBAPI.AccessToken = value;
        }
    }
    #endregion

    //Constructor
    public MyFB()
    {
        myFBAPI = new FacebookAPI();
    }

    public String GetFriends()
    {
        try
        {
            System.Diagnostics.Debug.WriteLine("Here0");
            String request_string = "/me/friends";
            JSONObject me = myFBAPI.Get(request_string);
            System.Diagnostics.Debug.WriteLine("Here1");

            String friends = "";
            JSONObject[] array_friends = me.Dictionary["data"].Array;
            for (int i = 0; i < array_friends.Length ; i++)
            {
                friends = friends + ((friends.Length > 0) ? "<p>" : "") + i.ToString() + "> " +array_friends[i].Dictionary["name"].String;
            }
            return friends;
        }
        catch (FacebookAPIException err)
        {
            return err.Message;
        }
    }

    public String GetMyName()
    {
        try
        {
            JSONObject me = myFBAPI.Get("/me");
            return me.Dictionary["name"].String;
        }
        catch (FacebookAPIException err)
        {
            return err.Message;
        }
    }

    public String GetPersonalInfo()
    {
        try
        {
            MyWebRequest myRequest = new MyWebRequest("https://graph.facebook.com/me?access_token=" + AccessToken, "GET");
            return myRequest.GetResponse();
        }
        catch (Exception err)
        {
            return err.Message;
        }
    }

    public String GetAccessToken(string code, string redirect_uri)
    {
        //create the constructor with post ty   pe and few data
        string request_paramteres = "client_id=" + this.ApplicationID + "&client_secret=" + this.ApplicationSecret + "&type=web_server" + "&code=" + code + "&redirect_uri=" + redirect_uri;
        MyWebRequest myRequest = new MyWebRequest("https://graph.facebook.com/oauth/access_token?" + request_paramteres, "GET");
        string accessToken = myRequest.GetResponse().Split('&')[0];
        accessToken = accessToken.Split('=')[1];
        return accessToken;
    }

}
